import request from '../../utils/request'

function judgePhone(phone, password) {
  //前端验证
  if (!phone) {
    wx.showToast({
      title: '手机号不能为空',
      icon: 'none'
    });
    return

  }
  console.log(phone);
  let phoneReg = /^1(3|4|5|6|7|8|9)\d{9}/
  if (!phoneReg.test(phone)) {
    wx.showToast({
      title: '手机号格式不正确',
      icon: 'none'

    });
    return
  }

  if (!password) {
    wx.showToast({
      title: '密码不能为空'
    })
    return
  }
  judgeCode(phone, password)
}

async function judgeCode(phone, password) {
  let res = await request('/login/cellphone', { phone, password, isLogin: true})
  if (res.code === 200) {
    wx.showToast({
      title: '登陆成功',
    });
    let localData = JSON.stringify(res.profile)
    wx.setStorageSync('userinfo', localData)
    wx.reLaunch({
      url:'/pages/personal/personal'
    })

  } else if (res.code === 400) {
    wx.showToast({
      title: '手机号错误',
    });
  } else if (res.code === 502) {
    wx.showToast({
      title: '密码错误',
      icon: 'none'
    });
  } else {
    wx.showToast({
      title: '登陆失败请重新尝试',
      icon: 'none'
    });
  }

}






module.exports = {
  judgePhone,
}